package com.example.controller;

import com.example.dto.DishesDTO;
import com.example.dto.SysStoreDTO;
import com.example.entity.CategoriesEntity;
import com.example.excel.ZyDishesExcel;
import com.example.service.CategoriesService;
import com.example.service.DishesService;

import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;
import com.example.utils.ExcelUtils;

/**
* 菜品表
*
* @author yyyyjinying 1020066395@qq.com
* @since 1.0-SNAPSHOT 2025-04-29
*/
@RestController
@RequestMapping("dishes")
@Tag(name = "菜品表")
public class DishesController {
    @Autowired
    private DishesService dishesService;

    @Autowired
    private CategoriesService categoriesService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @RequestMapping(value = "page", method = {RequestMethod.POST, RequestMethod.GET})
    @PreAuthorize("hasAuthority('sys:dishes:page')")
    public Result<PageData<DishesDTO>> page(@RequestParam Map<String, Object> params){
        PageData<DishesDTO> page = dishesService.page(params);

        return new Result<PageData<DishesDTO>>().ok(page);
    }

    @GetMapping("list") // baseService.get("/sys/dishes/list", {})
    @Operation(summary = "列表")
    @PreAuthorize("hasAuthority('sys:dishes:page')")
    public Result<List<DishesDTO>> list(@RequestParam Map<String, Object> params) {
        List<DishesDTO> list = dishesService.list(params);
        return new Result<List<DishesDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:dishes:info')")
    public Result<DishesDTO> get(@PathVariable("id") Long id){
        DishesDTO data = dishesService.get(id);
        CategoriesEntity categoriesEntity = categoriesService.selectById(data.getCategoryId());
        data.setCategoryName(categoriesEntity.getName());
        return new Result<DishesDTO>().ok(data);
    }

    @GetMapping("destail/{id}")
    @Operation(summary = "餐单详情")
    // @PreAuthorize("hasAuthority('sys:dishes:info')")
    public Result<DishesDTO> destail(@PathVariable("id") Long id){
        DishesDTO dishesDTO = dishesService.detailsByDishId(id);
        CategoriesEntity categoriesEntity = categoriesService.selectById(dishesDTO.getCategoryId());
        dishesDTO.setCategoryName(categoriesEntity.getName());
        return new Result<DishesDTO>().ok(dishesDTO);
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('sys:dishes:save')")
    public Result<String> save(@RequestBody DishesDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dishesService.save(dto);

        return new Result<String>().ok("ok");
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('sys:dishes:update')")
    public Result<String> update(@RequestBody DishesDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        dishesService.update(dto);

        return new Result<String>().ok("ok");
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('sys:dishes:delete')")
    public Result<String> delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        dishesService.delete(ids);

        return new Result<String>().ok("ok");
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @PreAuthorize("hasAuthority('sys:dishes:export')")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<DishesDTO> list = dishesService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "菜品表", list, ZyDishesExcel.class);
    }

}